package cn.org.timespace.easyframe.util.bean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {

	//只复制非空属性的方法
	public static void copyNotNullProperties(Object dest, Object orig)
			throws IllegalAccessException, InvocationTargetException {
		if (dest == null)
			throw new IllegalArgumentException("No destination bean specified");
		if (orig == null)
			throw new IllegalArgumentException("No origin bean specified");
		if (orig instanceof DynaBean) {
			DynaProperty origDescriptors[] = ((DynaBean) orig).getDynaClass()
					.getDynaProperties();
			for (int i = 0; i < origDescriptors.length; i++) {
				String name = origDescriptors[i].getName();
				if (PropertyUtils.isWriteable(dest, name)) {
					Object value = ((DynaBean) orig).get(name);

					if (value != null) {//只是这句与父类的copyProperties方法不一样
						copyProperty(dest, name, value);
					}
				}
			}

		} else if (orig instanceof Map) {
			for (Iterator names = ((Map) orig).keySet().iterator(); names
					.hasNext();) {
				String name = (String) names.next();
				if (PropertyUtils.isWriteable(dest, name)) {
					Object value = ((Map) orig).get(name);
					
					if (value != null) {//只是这句与父类的copyProperties方法不一样
						copyProperty(dest, name, value);
					}
				}
			}

		} else {
			PropertyDescriptor origDescriptors[] = PropertyUtils
					.getPropertyDescriptors(orig);
			for (int i = 0; i < origDescriptors.length; i++) {
				String name = origDescriptors[i].getName();
				if (!"class".equals(name)
						&& PropertyUtils.isReadable(orig, name)
						&& PropertyUtils.isWriteable(dest, name))
					try {
						Object value = PropertyUtils.getSimpleProperty(orig,
								name);
						
						if (value != null) {//只是这句与父类的copyProperties方法不一样
							copyProperty(dest, name, value);
						}
					} catch (NoSuchMethodException e) {
						e.printStackTrace();
					}
			}

		}
	}
}
